﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin.BlogManager
{
    public partial class BlogList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindBlogType();
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(BlogUPager);

            LoadCondition();

            string blogbigtype = ddlBlogBigType.SelectedValue;
            string blogtype = ddlBlogType.SelectedValue;
            string flag = ddlFlag.SelectedValue;
            string isrecommand = ddlIsRecommand.SelectedValue;

            //List<Member> list = new List<Member();
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();

            if (blogbigtype != "-1")
            {
                c.Add(CriteriaType.Equals, "BlogBigType", blogbigtype);
            }
            if (blogtype != "-1")
            {
                c.Add(CriteriaType.Equals, "BlogTypeID", blogtype);
            }
            if (flag != "-1")
            {
                c.Add(CriteriaType.Equals, "Flag", flag);
            }
            if (isrecommand != "-1")
            {
                c.Add(CriteriaType.Equals, "IsRecommand", isrecommand);
            }

            BlogUPager.ItemCount = bhelper.QueryBlogCountBySearch(c);

            // 以下的值从BasePage中得到
            BlogUPager.UrlFormat = UrlFormat;
            BlogUPager.PrefixText = GetPreFixText(BlogUPager);

            if (mark)
            {
                PagerInit(BlogUPager);
            }

            string[] fields = new string[] { "ID", "BlogBigType", "BlogTypeID", "Title", "BlogContent", "UsersID", "PVCount", "ReplyCount", "RecommandCount", "IsRecommand", "Flag", "CreateDate" };

            DataTable dt = new DataTable();
            dt = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, BlogUPager.Begin - 1, BlogUPager.Count, fields);

            dt.Columns.Add(new DataColumn("BlogType"));
            dt.Columns.Add(new DataColumn("strCreateDate"));
            dt.Columns.Add(new DataColumn("strFlag"));
            dt.Columns.Add(new DataColumn("strIsRecommand"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {

                    string blogtypeid = dr["BlogTypeID"] + "";
                    BlogType btinfo = new BlogType();
                    BlogTypeHelper bthelper = new BlogTypeHelper();
                    btinfo = bthelper.GetBlogType(blogtypeid);

                    dr["BlogType"] = btinfo.BlogTypeName;

                    DateTime dtCreateDate = Convert.ToDateTime(dr["createdate"]);
                    dr["strCreateDate"] = dtCreateDate.ToString("yyyy-MM-dd");

                    //加载状态
                    string getflag = dr["Flag"] + "";
                    string strflag = "";
                    if (getflag == "0")
                    {
                        strflag = "有效";
                    }
                    else if (getflag == "-9")
                    {
                        strflag = "系统屏蔽";
                    }
                    else if (getflag == "9")
                    {
                        strflag = "被删除";
                    }
                    dr["strFlag"] = strflag;


                    string getisrecommand = dr["IsRecommand"] + "";
                    string strisrecommand = "";
                    if (getisrecommand == "0")
                    {
                        strisrecommand = "否";
                    }
                    else if (getisrecommand == "1")
                    {
                        strisrecommand = "是";
                    }
                    dr["strIsRecommand"] = strisrecommand;
                }
            }

            rptBlog.DataSource = dt;
            rptBlog.DataBind();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }

        #region 列表事件
        protected void rptBlog_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");

            string id = hidID.Value;

            if (e.CommandName == "detail")//详情
            {
                //string url = "BlogDetail.aspx?id=" + id;
                //CommonClass.RedirectTargetUrl(PageNumber, url);
                Response.Redirect("BlogDetail.aspx?id=" + id);
            }
            else if (e.CommandName == "delete")//删除
            {
                Blog binfo = new Blog();
                BlogHelper bhelper = new BlogHelper();
                binfo = bhelper.GetBlog(id);
                binfo.Flag = -9;//系统屏蔽
                bhelper.EditBlog(binfo);

                Tools.Alert("屏蔽成功！");
            }
            else if (e.CommandName == "recommand")//推荐
            {
                Blog binfo = new Blog();
                BlogHelper bhelper = new BlogHelper();
                binfo = bhelper.GetBlog(id);
                binfo.IsRecommand = 1;
                bhelper.EditBlog(binfo);

                Tools.Alert("推荐成功！");
            }

            LoadContent(false);
        }
        #endregion

        #region 查询条件设置
        private void SetCondition()
        {
            string blogbigtype = ddlBlogBigType.SelectedValue;
            string blogtype = ddlBlogType.SelectedValue;
            string flag = ddlFlag.SelectedValue;
            string isrecommand = ddlIsRecommand.SelectedValue;

            //将查询条件放入Session中
            Session["QueryList"] =blogbigtype+"@"+ blogtype + "@" +flag+"@"+isrecommand+"@"+ "BlogList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 5)
            {
                string page = conditionAll[4];
                if (page == "BlogList")
                {
                    ddlBlogBigType.SelectedValue = conditionAll[0];
                    ddlBlogType.SelectedValue = conditionAll[1];
                    ddlFlag.SelectedValue = conditionAll[2];
                    ddlIsRecommand.SelectedValue = conditionAll[3];
                }
            }
        }
        #endregion

        #region 加载类别
        private void BindBlogType()
        {
            BlogTypeHelper bthelper = new BlogTypeHelper();

            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            List<BlogType> list = bthelper.QueryBlogTypeBySearch(c, null);
            ddlBlogType.DataSource = list;
            ddlBlogType.DataTextField = "BlogTypeName";
            ddlBlogType.DataValueField = "ID";
            ddlBlogType.DataBind();

            ListItem li = new ListItem();
            li.Value = "-1";
            li.Text = "-请选择-";
            ddlBlogType.Items.Insert(0, li);
        }
        #endregion

        #region 种类改变
        protected void ddlBlogBigType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string blogbigtype = ddlBlogBigType.SelectedValue;
            BlogTypeHelper bthelper = new BlogTypeHelper();

            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "BlogBigType", blogbigtype);
            List<BlogType> list = bthelper.QueryBlogTypeBySearch(c, null);
            ddlBlogType.DataSource = list;
            ddlBlogType.DataTextField = "BlogTypeName";
            ddlBlogType.DataValueField = "ID";
            ddlBlogType.DataBind();

            ListItem li = new ListItem();
            li.Value = "-1";
            li.Text = "-请选择-";
            ddlBlogType.Items.Insert(0, li);
        }
        #endregion
    }
}